package com.wkk.authorization.controller;

import com.wkk.annotation.AutoLog;
import com.wkk.authorization.common.AuthorizationConstant;
import com.wkk.authorization.dto.AddOrUpdateRoleDto;
import com.wkk.authorization.entity.UmsRole;
import com.wkk.authorization.service.UmsRoleService;
import com.wkk.authorization.service.UmsUserRoleService;
import com.wkk.constant.CommonConstant;
import com.wkk.log.common.LogConstant;
import com.wkk.utils.RedisUtil;
import com.wkk.verification.group.AddDto;
import com.wkk.verification.group.UpdateDto;
import com.wkk.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @author : WangKeKe
 * @date : 2020/10/5 12:45
 * Description : 角色控制类
 */

@RestController
@Slf4j
@RequestMapping("/ums/role")
@Api(tags = "RESTFUL-管理员-角色管理")
public class UmsRoleController {

    @Autowired
    private UmsRoleService roleService;

    @Autowired
    private UmsUserRoleService userRoleService;

    @Autowired
    private RedisUtil redisUtil;

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-角色管理-添加")
    @ApiOperation("角色管理-添加")
    @PostMapping("/add")
    public Result<String> addRole(@RequestBody @Validated(AddDto.class) AddOrUpdateRoleDto addOrUpdateRoleDto) {
        return roleService.addOrUpdateRole(addOrUpdateRoleDto) ? Result.ok("添加成功") : Result.error("添加失败");
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-角色管理-更新")
    @ApiOperation("角色管理-更新")
    @PutMapping("/update")
    public Result<String> updateRole(@RequestBody @Validated(UpdateDto.class) AddOrUpdateRoleDto addOrUpdateRoleDto) {

        return roleService.addOrUpdateRole(addOrUpdateRoleDto) ? Result.ok("修改成功") : Result.error("修改失败");
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-角色管理-根据ID删除")
    @ApiOperation("角色管理-单个删除")
    @ApiImplicitParam(name = "id", value = "角色id", required = true)
    @DeleteMapping("/delete/{id}")
    public Result<String> deleteRole(@PathVariable String id) {
        redisUtil.del(AuthorizationConstant.PERMISSION_CACHE_PREFIX + id);
        return roleService.removeById(id) ? Result.ok("删除成功!") : Result.error("删除失败");
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-角色管理-批量删除")
    @ApiOperation("角色管理-批量删除")
    @DeleteMapping(value = "/deleteBatch/{ids}")
    public Result<?> deleteBatch(@PathVariable String ids) {
        redisUtil.delKeyByCondition(AuthorizationConstant.ROLE_CACHE_PREFIX);
        return roleService.removeByIds(Arrays.asList(ids.split(","))) ? Result.ok("批量删除成功!") : Result.error("批量删除失败");
    }

    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-角色管理-全表查询")
    @ApiOperation("角色管理-所有角色")
    @GetMapping("/list")
    public Result<List<UmsRole>> queryAllList() {
        return Result.ok(roleService.list());
    }


    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-用户赋权-获取用户角色")
    @ApiOperation("用户赋权-获取用户角色")
    @GetMapping("/getUserRoles/{userId}")
    public Result<Set<String>> getUserRoles(@PathVariable String userId) {
        return Result.ok(roleService.queryRolesByUserId(userId));
    }

    // 获取包含某角色ID的所有用户信息


    @AutoLog(description = LogConstant.ROLE_NAME_ADMIN + "-用户赋权-角色修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户ID", required = true),
            @ApiImplicitParam(name = "roleIds", value = "角色ID组（逗号分隔）", required = true)
    })
    @ApiOperation("用户赋权-角色修改")
    @RequiresRoles(value = {"admin", "root"}, logical = Logical.OR)
    @PutMapping("/updateUserRoles/{userId}/{roleIds}")
    public Result<String> updateUserRoles(@PathVariable String userId, @PathVariable String roleIds) {
        // 这里需要对鉴权的缓存进行更新 - 移除
        redisUtil.del(CommonConstant.PREFIX_SHIRO + CommonConstant.SHIRO_REDIS_ROLE_PERMISSION + ":" + userId);
        return userRoleService.updateUserRole(userId, roleIds) ? Result.ok("修改成功!") : Result.error("修改失败");
    }

    // 创建一个带有某角色的用户


}
